/* Minimal main program -- everything is loaded from the library */

#include "Python.h"

#ifdef __FreeBSD__
#include <floatingpoint.h>
#endif

#include <cuda_runtime.h>
#include <cublas_v2.h>
//#include <shrUtils.h>
//#include <shrQATest.h>
//#include <cutil_inline.h>

int devID;
struct cudaDeviceProp props;


/**
 */
int initCUDA_orig()
{
    cudaGetDeviceCount(&devID);
    if (devID == 0) {
        fprintf(stderr, "There is no device.\n");
        return 0;
    }
    
    int i;
    for (i = 0; i < devID; i++) {
        if (cudaGetDeviceProperties(&props, i) == cudaSuccess) {
            if (props.major >= 1) {
                break;
            }
        }
    }
    
    if (i == devID) {
        fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
        return 0;
    }
    
    cudaSetDevice(i);
            
    return 1;
}

/*
void initCUDA() 
{
    cutilSafeCall( cudaSetDevice(cutGetMaxGflopsDeviceId()) );
    printf("CUDA initialized in Modules/python.c main().\n");
    
    // get number of SMs on this GPU
    cutilSafeCall(cudaGetDevice(&devID));
    cutilSafeCall(cudaGetDeviceProperties(&props, devID));
    
    printf("Device %d: \"%s\" with Compute %d.%d capability\n", 
            devID, props.name, props.major, props.minor);

}
*/

/* Python entrance!!!
 * But in pgenmain.c that is pgen entrance!
 */
int
main(int argc, char **argv)
{
	/* 754 requires that FP exceptions run in "no stop" mode by default,
	 * and until C vendors implement C99's ways to control FP exceptions,
	 * Python requires non-stop mode.  Alas, some platforms enable FP
	 * exceptions by default.  Here we disable them.
	 */
#ifdef __FreeBSD__
	fp_except_t m;

	m = fpgetmask();
	fpsetmask(m & ~FP_X_OFL);
#endif
    
    /** <CUDA initialization> */
    if(!initCUDA_orig()) {
        return 1;
    }

//    initCUDA();
    printf("CUDA initialized in Modules/python.c main().\n");
    /** </CUDA initialization> */
    
	return Py_Main(argc, argv);
}
